package library.console;

import com.j256.ormlite.logger.LocalLog;
import library.DaoUtil;
import library.controller.LibrarianController;
import library.controller.ReaderController;
import library.entity.Librarian;
import library.entity.Reader;

import java.sql.SQLException;
import java.util.Scanner;

public class MainMenu {
	private Librarian loginLibrarian;
	private LibrarianController librarianController;
	private static Scanner scanner;
	
	// System entry point ,do not write anything in main method
	public static void main(String args[]) throws SQLException {
		System.setProperty(LocalLog.LOCAL_LOG_LEVEL_PROPERTY, "ERROR");
		scanner = new Scanner(System.in);
		new MainMenu();
	}	
	
	//Special constructor for JUnit console testing
	public static void setScanner(Scanner otherScanner){
		scanner = otherScanner;		
	}
	
	//  Program start from here
	public MainMenu() throws SQLException {
		try {
			DaoUtil.init();
			this.librarianController = new LibrarianController();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			System.out.print("Startup fail.");
			e.printStackTrace();
			return;
		}

        LibrarianController librarianController = new LibrarianController();

        if(librarianController.getLibrarian("admin") == null) {
            System.out.println("No default user found. Creating default admin librarian and testreader....");
            Librarian adminLibrarian = new Librarian("admin", "admin");
            adminLibrarian.setAdminRight(true);
            librarianController.create(adminLibrarian);
            
            ReaderController r = new ReaderController();
            r.create(new Reader("testreader","xxx","98765432",""));
        }

		if(login()){
			showMainMenu();
        }
	}
	
	public boolean login(){
		int loginErrorCount = 0;
		System.out.println("============================");
		System.out.println("  System login ");
		System.out.println("============================");
        String userName,pwd;
        boolean loginCorrect;
		do{
            System.out.print("Enter Librarian Name: ");
            userName = scanner.nextLine();
            System.out.print("Enter Password: ");
            pwd = scanner.nextLine();

            if ((librarianController.check(userName, pwd) != null)){
                loginCorrect = true;
            } else {
                loginCorrect = false;
                loginErrorCount++;
            }
            if(!loginCorrect)
                System.out.println("Incorrect Librarian Name or password, please try again.");
		} while (!loginCorrect && loginErrorCount < 3);
        if(loginCorrect){
            loginLibrarian = librarianController.check(userName, pwd);
            return true;
        } else {
            System.out.println("ERROR: Login failure over 3 times. Exiting.........");
            return false;
        }
	}
	
	public void showMainMenu(){
		int input;
		do{
            String options[] = {"Borrow","Return books","Administration","Quit"};
            input = ConsoleUtilities.showMenu(scanner, "Main Menu", options);
			switch (input) {
				case 3:
					if(loginLibrarian.isAdmin())
						new AdminMenu(scanner);
					else {
						System.out.println("You don't have the permission to enter administration menu, press any key to return main menu.");
                        scanner.nextLine();
                    }
					break;
				case 1:
					new BorrowMenu(scanner);
					break;
				case 2:
					BorrowMenu.returnBook(scanner);
					break;
			}
		} while(input != 4);
	}
}
